<?php

function postInvoke($url, $hostName, $params){
	
	$postParams = array();
	foreach ($params as $key=>$val) {
		$post_params[] = $key.'='.urlencode($val);
	}

	$postString = implode('&', $post_params);
	//var_dump($postString);
	$result = '';
    if (function_exists('curl_init')) {
    	$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $hostName));
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, $postString);
        // https
        if (substr($url,0,5)=='https') {
            ## Below two option will enable the HTTPS option.
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  FALSE);
        }
       // var_dump($url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'LESHI.COM BC Test PHP5 Client ver: ' . phpversion());
        $result = curl_exec($ch);
        curl_close($ch);
    } else {
        $context = array('http' => array('method' => 'POST',
                                         'header' => 'Content-type: application/x-www-form-urlencoded'    . "\r\n".
										 'Host: '.$hostName . "\r\n".
                                         'User-Agent: YOUKU.COM BC Test PHP5 Client ver: ' . phpversion() . "\r\n".
                                         'Content-length: ' . strlen($postString),
                                         'content' => $postString));
        $contextId = stream_context_create($context);
        $handle = fopen($url, 'r', false, $contextId);
        if ($handle) {
            while (!feof($handle)) $result .= fgets($sock, 4096);
            fclose($handle);
        }
    }

	return $result;	
}

/**
 * 氓藛鈥好ヂ宦好久ヂ惵�
 * @params 氓戮鈥γ久ヂ惵嵜︹�掳忙聧庐茂录藛氓鈥β趁佲�忙鈥⒙懊烩�茂录鈥�
 * @secretKey String 莽颅戮氓聬聧氓炉鈥犆┾�楼
 * @return String 
 */
function createSign(array $params, $secretKey, $algo = "md5"){
	ksort($params);
	reset($params);
	
	$signPars = '';
    while(list($k, $v) = each($params)){
        if('' === $v) continue;
        $signPars .= $k . '=' . $v . '&';
    }

    // 氓沤禄忙沤鈥懊ε撯偓氓聬沤盲赂鈧ぢ嘎�"&"
    $signPars = substr($signPars, 0, strlen($signPars)-1);
	$sign = strtolower(hash_hmac($algo, $signPars, $secretKey));
	
	return $sign;
}



?>
